<?php
namespace App\Http\Controllers\Admin;

use App\Models\AdminUserLog;
use App\Models\NewAdmin;
use App\Services\AuthService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use App\Services\CommonService;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redis;

class LoginController extends BaseVerifyController {
    /**
     * 登陆
     */
    public function login(Request $request){
        $mobile=$request->input('mobile');
        $password=$request->input('password');
        if(!$mobile || !$password){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'手机号或密码不能为空');
        }
        $user = NewAdmin::where(['mobile'=>$mobile,'is_delete'=>CommonService::IS_DELETE_NOT,'type'=>AuthService::MODEL_ADMIN])->first();
        $user = obToArray($user);
        if($user){
            if(CommonService::USER_STATUS_NOT == $user['status']){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'账号已冻结');
            }
            if(md5($mobile.$password) == $user['password']){
                $token = createToken(AuthService::MODEL_ADMIN);
                $res = NewAdmin::where('id',$user['id'])->update(['token'=>$token]);
                if($res){
                    if($user['allow_ip']&&!in_array(getRealIp(),explode(',',$user['allow_ip']))){
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'ip地址受限');
                    }
                    Redis::del($user['token']);
                    $user['token'] = $token;
                    Redis::setEx($token, 72000*30, json_encode($user));
                    AdminUserLog::addLog($user['id'],'登录',$request->all(),AdminUserLog::OPERATION_TYPE_LOGIN);
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'登录成功',['token'=>$token]);
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'登录失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'密码错误');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'帐号不存在');
        }
    }

    /**
     * 退出登陆
     */
    public function loginOut(Request $request){
        $userToken = $request->header("Authorization");
        if ($userToken) {
            $user = NewAdmin::where(['token' => $userToken, 'type' => AuthService::MODEL_ADMIN])->first();
            $user = obToArray($user);
            if ($user) {
                Redis::del($userToken);
                NewAdmin::where('id', $user['id'])->update(['token' => '']);
                AdminUserLog::addLog($user['id'], '退出登录', $request->all(), AdminUserLog::OPERATION_TYPE_LOGIN_OUT);
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '退出成功');
    }

    /**
     * 忘记密码
     * @param Request $request
     * @return false|string
     */
    public function forgetPwd(Request $request){
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'入口关闭');
        $mobile = $request->input('mobile','');
        $code = $request->input('code','');
        $pwd = $request->input('pwd','');

        if(!$mobile ||!$code || !$pwd){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }

        $checkUser  = NewAdmin::where(['mobile'=>$mobile])->select('id','mobile')->first();
        $checkUser = $checkUser?$checkUser->toArray():[];
        if(!$checkUser){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'用户不存在');
        }
        $checkCode = SmsService::checkCode($mobile,$code);
        if(!$checkCode){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码错误');
        }
        $data['password'] = md5($checkUser['mobile'].$pwd);
        $res = NewAdmin::where(['id'=>$checkUser['id']])->update($data);
        if($res){
            AdminUserLog::addLog($checkUser['id'],'忘记密码修改成功',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

}


